package com.example.asus.mydiary;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.asus.db.domain.Note;
import com.example.asus.db.gen.NoteDao;
import com.example.asus.db.service.GreenDaoManager;
import com.example.asus.db.service.SysApplication;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AddDiary extends AppCompatActivity {
    private List<Note> noteList;
    private Note note;
    private NoteDao noteDao;
    private EditText etTitle;
    private EditText etBody;
    private AutoSplitTextView autuBody;
    private JustifyTextView jtBody;
    private Date date;
    private String sDate;
    private FloatingActionButton floatingActionButton;
    private DiaryRecyclerAdapter diaryRecyclerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_diary);
        SysApplication.getInstance().addActivity(this);
        init();

    }

    //初始化控件
    public void init(){
        etTitle = (EditText)findViewById(R.id.add_title);
        etBody = (EditText)findViewById(R.id.add_body);
//        autuBody = (AutoSplitTextView)findViewById(R.id.add_autobody);
//        jtBody = (JustifyTextView)findViewById(R.id.add_jtBody);
    }

    /**
     * Back键 —— 返回主页面
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent(AddDiary.this,DiaryRecycler.class);
        startActivity(intent);
    }

    /**
     *  初始化菜单
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_save:
                /**
                 *  准备数据库
                 */
                GreenDaoManager.InitDao(AddDiary.this,"diary.db");
                noteDao = GreenDaoManager.getDaoSession().getNoteDao();

                /**
                 *  添加数据到数据库中
                 */
                note = new Note();
                note.setTitle(etTitle.getText().toString());
                note.setBody(etBody.getText().toString());
                date = new Date();
                //大写HH为24小时，小写hh为12小时
                sDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
                note.setDate(sDate);  //设置当前时间

                /**
                 *  判断内容是否为空，若为空则不能提交
                 */
                if(note.getBody().length()==0){
                    Toast.makeText(AddDiary.this,"日记内容不能为空",Toast.LENGTH_LONG).show();
                }
                else{
                    noteDao.insert(note);
                    /**
                     *  返回到日记页面
                     */
                    Intent intent = new Intent(AddDiary.this,DiaryRecycler.class);
                    startActivity(intent);
                    finish();
                }
                break;
            case R.id.menu_return:
                Intent intent = new Intent(AddDiary.this,DiaryRecycler.class);
                startActivity(intent);
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
